<?php
namespace Admin\Controller;
use Think\Controller;
class YuyueController extends BaseController
{
    protected $comeModel;
    protected $memberModel;
    public function __construct()
    {
        parent::__construct();
       $this->comeModel = M('come');
       $this->memberModel = D('member');
    }
    public function add()
    {
    	if(IS_POST)
    	{
    		$model = D('Yuyue');
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
    				$this->success('添加成功！', U('lst?p='.I('get.p')));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}

		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '添加瑜伽馆预约',
			'_page_btn_name' => '瑜伽馆预约列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display();
    }
    public function edit()
    {
    	$id = I('get.id');
    	if(IS_POST)
    	{
    		$model = D('Yuyue');
    		if($model->create(I('post.'), 2))
    		{
    			if($model->save() !== FALSE)
    			{
    				$this->success('修改成功！', U('lst', array('p' => I('get.p', 1))));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
    	$model = M('Yuyue');
    	$data = $model->find($id);
    	$this->assign('data', $data);

		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '修改瑜伽馆预约',
			'_page_btn_name' => '瑜伽馆预约列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display();
    }
    public function delete()
    {
    	$model = D('Yuyue');
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
    		$this->success('删除成功！', U('lst', array('p' => I('get.p', 1))));
    		exit;
    	}
    	else 
    	{
    		$this->error($model->getError());
    	}
    }
    public function lst()
    {
    	$model = D('Yuyue');
    	$data = $model->search();
    	$this->assign(array(
    		'data' => $data['data'],
    		'page' => $data['page'],
    	));

		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '瑜伽馆预约列表',
			'_page_btn_name' => '添加瑜伽馆预约',
			'_page_btn_link' => U('add'),
		));
    	$this->display();
    }

    public function info()
    {
        $data = D('yuyue')->where(['add_time'=>'0'])->order("id asc")->find();

        if($data){
            D('yuyue')->where(array('id'=>$data['id']))->save(['add_time'=>1]);

        }
        die(json_encode($data['id']));

    }

    /**
     * @name 会员入场
     * @desc 会员入场
     * @auther ycp
     */
    public function admission()
    {
        if(IS_AJAX){
            $memberModel = D('member');
            $data = I('post.');
            $kahao = I('post.kahao');

            $memberInfo = $memberModel->where([
                'kahao' => ['eq',$kahao]
            ])->find();
            if($data['type'] == 1){
                $data['type'] ='会员';
            }else{
                $data['type'] ='教员';
            }
            $data['member_id'] = isset($memberInfo['id']) ? $memberInfo['id']:'';
            $data['time'] =time();
            if($memberInfo){
                $comeModel = M('come');
                if($comeModel->create($data,1)){
                    if($id=$comeModel->add()){
                        $data =$comeModel->find($id);
                        $this->ajaxReturn(['status'=>'success','msg'=>'入场成功','data'=>$data]);
                    }

                }
            }

        }
        $this->assign(array(
            '_page_title' => '瑜伽馆会员入场',
            '_page_btn_name' => '瑜伽馆会员入场',
        ));
        $this->display();
    }

    /**
     * @name 判断会员卡号
     * @desc
     * @auther ycp
     */
    public function checkCode()
    {
        if(IS_AJAX){
            $code = I('post.code');
            $res = $this->memberModel->where([
                'kahao' => ['eq',$code]
            ])->select();
            if($res){
                $this->ajaxReturn(['status'=>'success','msg'=>'卡号正确','data'=>$res]);
            }else{
                $this->ajaxReturn(['status'=>'error','msg'=>'卡号错误或者卡号不存在！']);
            }
        }
    }
}